import {flow, makeAutoObservable} from 'mobx';
import {request} from '../../utils/request';
import {load} from '../../utils/Storage';
import Loading from '../../components/widget/Loading';
const SIZE = 10;
export default class ShopStore {
  page: number = 1;

  goodList: GoodsSimple[] = [];

  refreshing: boolean = false;

  categoryList: GoodsCategory[] = [];

  constructor() {
    makeAutoObservable(this);
  }
  resetPage = () => {
    this.page = 1;
  };
  requestShopList = flow(function* (this: ShopStore) {
    if (this.refreshing) {
      return;
    }
    Loading.show();
    try {
      this.refreshing = true;
      const params = {
        page: this.page,
        size: SIZE,
      };
      const {data} = yield request('goodsList', params);
      if (data?.length) {
        if (this.page === 1) {
          this.goodList = data;
        } else {
          this.goodList = [...this.goodList, ...data];
        }
        this.page = this.page + 1;
      } else {
        if (this.page === 1) {
          this.goodList = [];
        } else {
          // 已经加载完了，没有更多数据
        }
      }
    } catch (error) {
      console.log(error);
    } finally {
      this.refreshing = false;
      Loading.hide();
    }
  }).bind(this);
  requestTop10Category = flow(function* (this: ShopStore) {
    try {
      const {data} = yield request('top10Category', {});
      this.categoryList = data || [];
    } catch (error) {
      console.log(error);
    }
  });
}
